package com.miao.teacher.ui;

import org.json.JSONException;
import org.json.JSONObject;

import com.espeaker.sdk.Constants;
import com.espeaker.sdk.api.ReviseApi;
import com.espeaker.sdk.model.GoldTopic;
import com.espeaker.sdk.utils.JsonUtils;
import com.google.gson.Gson;
import com.miao.teacher.models.Tpo;
import com.miao.teacher.utils.SeekBarPlayer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class QuestionViewActivity extends Activity {
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		if (mSeekBarPlayer != null) {
			mSeekBarPlayer.release();
		}
		finish();
	}

	private String AUDIO_Url2 = "http://static.yishuodian.com/sounds/tpo/test{0}/speaking_question{1}_dialog.mp3";
	private static final String EXTRA_TPO_TITLE = "extra.tpo.title";

	private String title = "";

	private SeekBarPlayer mSeekBarPlayer = null;
	private Button btnPlay = null;
	private Button btnPre = null;
	private Button btnNext = null;
	private SeekBar sBar = null;

	private ReviseApi api = new ReviseApi();

	/*
	 * 创建实例
	 */
	public static void createInstance(Context context, String title) {
		Intent intent = new Intent();
		intent.setClass(context, QuestionViewActivity.class);
		intent.putExtra(EXTRA_TPO_TITLE, title);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_default);
		if (getIntent().hasExtra(EXTRA_TPO_TITLE)) {
			title = getIntent().getStringExtra(EXTRA_TPO_TITLE);
		}
		api.getTimu(title, mHandler);
	}

	// UI消息 更新UI
	protected Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {// 此方法在ui线程运行
			switch (msg.what) {
			case Constants.REVISE_GETQUESTION:
				convertQuestion(msg.obj);
				break;
			}
		}
	};

	private void convertQuestion(Object obj) {
		try {
			JSONObject json = new JSONObject(obj.toString());
			int status = JsonUtils.getInt(json, "status", -1);
			if (status == 0) {
				JSONObject json2 = JsonUtils
						.getJSONObject(json, "values", null);
				JSONObject Question = JsonUtils.getJSONObject(json2, "timu",null);
				if (title.startsWith("G")) {
					setGold(JsonUtils.getString(Question, "question"));
				} else if (title.startsWith("T")) {
					Gson gson = new Gson();
					Tpo tpo = gson.fromJson(Question.toString(), Tpo.class);
					if (tpo.question_order == 1 || tpo.question_order == 2) {
						setGold(tpo.question);
					} else if (tpo.question_order == 3 || tpo.question_order == 4) {
						setQuestion34(tpo);
					} else if (tpo.question_order == 5 || tpo.question_order == 6) {
						setQuestion56(tpo);
					}
				}else
				{
					setGold(JsonUtils.getString(Question, "question"));
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void setQuestion34(Tpo tpo) {
		setContentView(R.layout.layout_question_34);
		((TextView) findViewById(R.id.tvTitle)).setText(title);
		((TextView) findViewById(R.id.tvQuestion)).setText(tpo.question);
		((TextView) findViewById(R.id.tvContent)).setText(tpo.content);
		((TextView) findViewById(R.id.tvContentTitle))
				.setText(tpo.content_title);
		findViewById(R.id.lly_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}

		});
		final String url = AUDIO_Url2.replace("{1}", tpo.question_order + "")
				.replace("{0}", tpo.tpo_order + "");
		;
		sBar = (SeekBar) findViewById(R.id.sBar);
		btnPlay = (Button) findViewById(R.id.btnPlayerPlay);
		btnPre = (Button) findViewById(R.id.btnPlayerPre);
		btnNext = (Button) findViewById(R.id.btnPlayerNext);
		SeekBarPlayer.setHandler(mHandler);
		btnPlay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer == null)
					return;
				if (mSeekBarPlayer.isReady()) {
					if (mSeekBarPlayer.isPlaying()) {
						mSeekBarPlayer.pause();
						arg0.setBackgroundResource(R.drawable.iv_audio_play);
					} else {
						mSeekBarPlayer.play();
						arg0.setBackgroundResource(R.drawable.iv_audio_pause);
					}
				}
			}
		});
		btnPre.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer == null)
					return;
				if (mSeekBarPlayer.isReady()) {
					mSeekBarPlayer.backward();
				}
			}
		});
		btnNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer == null)
					return;
				if (mSeekBarPlayer.isReady()) {
					mSeekBarPlayer.forward();
				}

			}
		});
		sBar.postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mSeekBarPlayer = SeekBarPlayer.create(url, sBar);
				mSeekBarPlayer
						.setOnCompletionListener(new OnCompletionListener() {
							@Override
							public void onCompletion(MediaPlayer arg0) {
								mSeekBarPlayer.seekTo(0);
								btnPlay
										.setBackgroundResource(R.drawable.iv_audio_play);
							}
						});
			}

		}, 500);
	}

	private void setQuestion56(Tpo tpo) {
		setContentView(R.layout.layout_question_56);
		((TextView) findViewById(R.id.tvTitle)).setText(title);
		((TextView) findViewById(R.id.tvQuestion)).setText(tpo.question);
		findViewById(R.id.lly_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}

		});
		final String url = AUDIO_Url2.replace("{1}", tpo.question_order + "")
				.replace("{0}", tpo.tpo_order + "");
		;
		sBar = (SeekBar) findViewById(R.id.sBar);
		btnPlay = (Button) findViewById(R.id.btnPlayerPlay);
		btnPre = (Button) findViewById(R.id.btnPlayerPre);
		btnNext = (Button) findViewById(R.id.btnPlayerNext);
		SeekBarPlayer.setHandler(mHandler);
		sBar.postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mSeekBarPlayer = SeekBarPlayer.create(url, sBar);
				mSeekBarPlayer
						.setOnCompletionListener(new OnCompletionListener() {
							@Override
							public void onCompletion(MediaPlayer arg0) {
								mSeekBarPlayer.seekTo(0);
								btnPlay
										.setBackgroundResource(R.drawable.iv_audio_play);
							}
						});
			}

		}, 500);
		btnPlay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer == null)
					return;
				if (mSeekBarPlayer.isReady()) {
					if (mSeekBarPlayer.isPlaying()) {
						mSeekBarPlayer.pause();
						arg0.setBackgroundResource(R.drawable.iv_audio_play);
					} else {
						mSeekBarPlayer.play();
						arg0.setBackgroundResource(R.drawable.iv_audio_pause);
					}
				}
			}
		});
		btnPre.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer == null)
					return;
				if (mSeekBarPlayer.isReady()) {
					mSeekBarPlayer.backward();
				}
			}
		});
		btnNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer == null)
					return;
				if (mSeekBarPlayer.isReady()) {
					mSeekBarPlayer.forward();
				}

			}
		});
	}

	/*
	 * 设置1-2题 题目查看
	 */
	private void setGold(String question) {
		setContentView(R.layout.layout_question_12);
		((TextView) findViewById(R.id.tvTitle)).setText(title);
		((TextView) findViewById(R.id.tvQuestion)).setText(question);
		findViewById(R.id.lly_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}

		});
	}
}
